package com.outsourcing.bottle.adapter;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.BottleTypeTable;
import com.outsourcing.bottle.domain.BottleEntry;
import com.outsourcing.bottle.domain.BottleFeedEntry;
import com.outsourcing.bottle.domain.BottleLikeEntry;
import com.outsourcing.bottle.domain.BottleTypeEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.BottleDetailActivity;
import com.outsourcing.bottle.ui.ChooseFriendActivity;
import com.outsourcing.bottle.ui.ExpandEditActivity;
import com.outsourcing.bottle.ui.HomeActivity;
import com.outsourcing.bottle.ui.MapViewActivity;
import com.outsourcing.bottle.ui.PhotoDetailActivity;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.ui.fragment.BottleFragment;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.widget.CustomGridView;

public class PublicActivityAdapter extends BaseAdapter {
	protected static final String TAG = "PublicActivityAdapter";

	private HomeActivity context;
	private BottleFragment bottleFragment;
	private BottleTypeEntry entry = null;
	public static final String TEXT_FORMAT = "<font color='#1479ad'>%s</font> use Common net to gain Ms.faith's Memory Bottle <font color='#000'># 9 minutes ago</font>";
	public static final String TEXT_THROW_TIPS = "<font color='#1479ad'>Tips:</font>";
	public static final String TEXT_ADDFRD_FORMAT_WITHFROM = "<font color='#1479ad'>%s</font> 与 <font color='#1479ad'>%s</font> 成为了好友";
	public static final String TEXT_ADDFRD_NOFROM = "与 <font color='#1479ad'><b>%s</b></font> 成为了好友";
	private BottleTypeTable btt = null;
	private List<BottleEntry> msgs;
	// private BottleTimelineInfo bottleTimelineInfo;
	public ImageLoader imageLoader;
	
	private ImageLoader btConfigLoader;

	public boolean isHasHeader;

	public PublicActivityAdapter(HomeActivity context, BottleFragment bottleFragment, List<BottleEntry> msgs, boolean isHasHeader) {
		super();
		this.isHasHeader = isHasHeader;
		this.context = context;
		this.bottleFragment = bottleFragment;
		this.msgs = msgs;
		imageLoader = new ImageLoader(context, AppContext.BottleTimelineIcon);
		btConfigLoader = new ImageLoader(context, AppContext.BOTTLE_CONFIG);
		btt = new BottleTypeTable();

	}

	@Override
	public int getCount() {
		return msgs.size();
	}

	@Override
	public Object getItem(int position) {
		return msgs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	ViewHolder holder = null;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null || ((ViewHolder) convertView.getTag()).flag != position) {
			holder = new ViewHolder();
			holder.flag = position;
			// Item layout
			convertView = LayoutInflater.from(context).inflate(R.layout.bottle_list_avatar_item, null);
			// author img
			holder.mAuthorView = (RemoteImageView) convertView.findViewById(R.id.iv_author_photo);
			// bottle img
			holder.mBottleType = (ImageView) convertView.findViewById(R.id.iv_bottle_type);
			// content layout
			LinearLayout contentLayout = (LinearLayout) convertView.findViewById(R.id.feed_post_body);

			View view = LayoutInflater.from(context).inflate(
					R.layout.bottle_list_detail_item, null);

			holder.mNickname = (TextView) view.findViewById(R.id.tv_nickname);
			holder.mLocation = (TextView) view.findViewById(R.id.tv_location);

			holder.mTag = (TextView) view.findViewById(R.id.tv_tag);
			holder.mCopyTips = (TextView) view.findViewById(R.id.tv_copy_info);
			holder.mood = (TextView) view.findViewById(R.id.tv_mood);

			holder.mBottleContent = (RelativeLayout) view.findViewById(R.id.rl_bottle_content);
			holder.mMessage = (TextView) view.findViewById(R.id.tv_doing);
			// photo
			holder.mPhotoWrapper = (LinearLayout) view.findViewById(R.id.rl_bottle_content_photo);
			holder.mPhotoView = (ImageView) view.findViewById(R.id.photo);

			holder.bt_drawing = (Button)view.findViewById(R.id.bt_drawing); 
			holder.bt_allow_draw = (Button)view.findViewById(R.id.bt_allow_draw);
			
			holder.mLockedPhotoLayout = (LinearLayout) view.findViewById(R.id.ll_bottle_photo_locked);
			holder.mLockedPhoto = (ImageView) view.findViewById(R.id.iv_locked_photo);
			holder.mLockedPhotoTip = (Button) view.findViewById(R.id.bt_locked_photo_tips);

			// 喜欢popup
			holder.mLikeLayout = (RelativeLayout) view.findViewById(R.id.comment_button_like);

			holder.mLikeIcon = (ImageView) view.findViewById(R.id.comment_button_like_icon);
			holder.mLikeText = (TextView) view.findViewById(R.id.comment_button_like_text);

			// 瓶子没打开的区域
			holder.mUnopened_content = (LinearLayout) view.findViewById(R.id.ll_bottle_unopened_content);
			holder.mOpen_tips = (TextView) view.findViewById(R.id.tv_bottle_open_tips);
			holder.mOpen = (Button) view.findViewById(R.id.bt_bottle_open);
			holder.mDrop = (Button) view.findViewById(R.id.bt_bottle_drop);

			holder.mFeedComment = (RelativeLayout) view.findViewById(R.id.feed_comments);
			holder.mComment = (Button) view.findViewById(R.id.bt_bottle_comment);

			holder.mCopy = (Button) view.findViewById(R.id.bt_bottle_copy);
			holder.mForward = (Button) view.findViewById(R.id.bt_bottle_forward);

			holder.mLikeGrid = (CustomGridView) view.findViewById(R.id.ll_comment_like_grid);
			holder.line_grid = (View) view.findViewById(R.id.line_like_grid);
			
			holder.line_bt_comment = (View) view.findViewById(R.id.line_bt_comment);
			
			holder.mfeed_item_comment_more = (RelativeLayout) view.findViewById(R.id.feed_comments_more);
			holder.comments_ellipsis_text = (TextView) view.findViewById(R.id.comments_ellipsis_text);
			holder.mfeed_comment_layout = (LinearLayout) view.findViewById(R.id.ll_feed_comment_layout);
			
			holder.mOp_layout = (LinearLayout) view.findViewById(R.id.ll_botle_op);
			holder.mOpAvatar = (RemoteImageView)view.findViewById(R.id.bt_opavatar);
			holder.mOp_location = (LinearLayout) view.findViewById(R.id.ll_botle_op_location);
			holder.mFeedContent = (TextView) view.findViewById(R.id.tv_feed_content);
			holder.mOplocation = (TextView) view.findViewById(R.id.tv_oplocation);
			holder.mOpDistance = (TextView) view.findViewById(R.id.tv_distance);
			contentLayout.addView(view);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			final BottleEntry message = (BottleEntry) getItem(position);

			try {
				holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_big);
				holder.mAuthorView.setImageUrl(message.getBt_avatar());
				
				entry = btt.getBottleType(message.getBttypeid());
				btConfigLoader.DisplayImage(entry.getBttype_sjdicon(), holder.mBottleType, R.drawable.bottle_type_1);
			} catch (Exception e) {
				e.printStackTrace();
				holder.mAuthorView.setImageResource(R.drawable.avatar_default_big);
			}

			holder.mAuthorView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					int ouid = message.getBt_uid();
					Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
					spaceIntent.putExtra("ouid", ouid);
					context.startActivity(spaceIntent);
				}
			});

			holder.mBottleType.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					bottleFragment.doSendSingleBtPri(message.getBttypeid(), message.getBtid());
				}
			});

			String nickName = null;
			String feed = (message.getBt_feed() != null && message.getBt_feed()
					.length() > 0) ? ("<font color='#000000'>"
					+ message.getBt_feed() + "</font>") : "";
			if (message.getBt_uid() != AppContext.getInstance().getLogin_uid()) {
				nickName = (message.getBt_nickname() != null && message
						.getBt_nickname().length() > 0) ? ("<font color='#DA4A37'>"
						+ message.getBt_nickname() + "</font>") : "";
			} else {
				nickName = (message.getBt_nickname() != null && message
						.getBt_nickname().length() > 0) ? ("<font color='#3F99D8'>"
						+ message.getBt_nickname() + "</font>") : "";
			}
			// holder.mNickname.setText(Html.fromHtml(nickName) + " "
			// + message.getBt_feed() + " " + Html.fromHtml(time));

			holder.mNickname.setText(Html.fromHtml(nickName + feed));

			if (!TextUtils.isEmpty(message.getBt_location())&& message.getBt_location().trim().length()>0) {
				holder.mLocation.setVisibility(View.VISIBLE);
				String time = (!TextUtils.isEmpty(message.getBt_time())) ? ("<font color='#666666'>"+
								"#"+message.getBt_time() + "</font>") : "";		
				holder.mLocation.setText(Html.fromHtml(time+" @"+message.getBt_location()));
				if (message.getBt_showaddress() == 1 && message.getBt_open_drop_show() != 1) {
					holder.mLocation.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent bottleDetailIntent = new Intent(context, BottleDetailActivity.class);
							bottleDetailIntent.putExtra("btid", message.getBtid());
							bottleDetailIntent.putExtra("type", 2);// 1，LikeList2,location
							context.startActivity(bottleDetailIntent);
						}
					});
				}
			} else {
				holder.mLocation.setVisibility(View.GONE);
			}

			if (!TextUtils.isEmpty(message.getBt_tag())) {
				holder.mTag.setVisibility(View.VISIBLE);
				String locationTips = "<font color='#DA4A37'>" + TextUtil.R("bottle_txt_location_tips") + "</font>";
				holder.mTag.setText(Html.fromHtml(locationTips + TextUtil.htmlEncode(message.getBt_tag())));
				
			} else {
				holder.mTag.setVisibility(View.GONE);
			}

			if (!TextUtils.isEmpty(message.getBt_copy_info())) {
				holder.mCopyTips.setVisibility(View.VISIBLE);
				String commentTips = "<font color='#DA4A37'>" + TextUtil.R("bottle_txt_forward_tips") + "</font>";
				holder.mCopyTips.setText(Html.fromHtml(commentTips +  TextUtil.htmlEncode(message.getBt_copy_info())));
			} else {
				holder.mCopyTips.setVisibility(View.GONE);
			}

			if (!TextUtils.isEmpty(message.getBt_doing())) {
				holder.mood.setVisibility(View.VISIBLE);
				String moodTips = "<font color='#DA4A37'>" + TextUtil.R("bottle_txt_mood_tips") + "</font>";
				holder.mood.setText(Html.fromHtml(moodTips + TextUtil.htmlEncode(message.getBt_doing())));
			} else {
				holder.mood.setVisibility(View.GONE);
			}

			if (!TextUtils.isEmpty(message.getBt_content())) {
				String messageTips = "<font color='#DA4A37'>" + TextUtil.R("bottle_txt_copy_tips") + "</font>";
				holder.mMessage.setVisibility(View.VISIBLE);
				holder.mMessage.setText(Html.fromHtml(messageTips + TextUtil.htmlEncode(message.getBt_content())));
			} else {
				holder.mMessage.setVisibility(View.GONE);
			}
			
			if (!TextUtils.isEmpty(message.getBt_opfeed())) {
				holder.mOp_layout.setVisibility(View.VISIBLE);
				if (message.getBt_open_drop_show() != 1) {
					holder.mOp_layout
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									Intent bottleDetailIntent = new Intent(context, BottleDetailActivity.class);
									bottleDetailIntent.putExtra("btid", message.getBtid());
									bottleDetailIntent.putExtra("type", 2);// 1，LikeList2,location
									context.startActivity(bottleDetailIntent);
								}
							});
				}

				
				try {
					holder.mOpAvatar.setDefaultImage(R.drawable.avatar_default_small);
					holder.mOpAvatar.setImageUrl(message.getBt_opavatar());
					holder.mOpAvatar.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
									spaceIntent.putExtra("ouid", message.getBt_opuid());
									context.startActivity(spaceIntent);
								}
							});
				} catch (Exception e) {
					e.printStackTrace();
					holder.mOpAvatar.setImageResource(R.drawable.avatar_default_small);
				}
				
				String onickName = (!TextUtils.isEmpty(message.getBt_opnickname())) ? ("<font color='#3F99D8'>"
						+ message.getBt_opnickname() + "</font>")
						: "";
				String otime = (!TextUtils.isEmpty(message.getBt_optime())) ? ( "#" + message.getBt_optime()) : "";
						
				holder.mFeedContent.setText(Html.fromHtml(onickName
						+ TextUtil.htmlEncode(message.getBt_opfeed())));
				if (!TextUtils.isEmpty(message.getBt_oplocation())) {
					holder.mOp_location.setVisibility(View.VISIBLE);
					holder.mOplocation.setText(otime + " @"+message.getBt_oplocation());
					holder.mOpDistance.setText(TextUtil.R("bottle_txt_comment_distance")+" "+message.getBt_opdistance());
				} else {
					holder.mOp_location.setVisibility(View.GONE);
				}

			} else {
				holder.mOp_layout.setVisibility(View.GONE);
			}
			

			if (message.getBt_open_drop_show() == 1) {
				holder.mood.setVisibility(View.GONE);
				holder.mMessage.setVisibility(View.GONE);
				holder.mLikeLayout.setVisibility(View.GONE);
				holder.mBottleContent.setVisibility(View.GONE);
				holder.mUnopened_content.setVisibility(View.VISIBLE);
				holder.mOpen_tips.setText(TextUtil.R("bottle_txt_open_tips") + " " + message.getBt_nickname());

				holder.mOpen.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						bottleFragment.doOpenBottle(message.getBtid(), position);
					}
				});
				holder.mDrop.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						bottleFragment.doDropBottle(message.getBtid(), position);
					}
				});

			} else {
				if (message.getBt_showaddress() == 1) {
					holder.mTag.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent bottleDetailIntent = new Intent(context, BottleDetailActivity.class);
							bottleDetailIntent.putExtra("btid", message.getBtid());
							bottleDetailIntent.putExtra("type", 2);// 1，LikeList2,location
							context.startActivity(bottleDetailIntent);
						}
					});				
				}
				holder.mood.setVisibility(View.VISIBLE);
				holder.mMessage.setVisibility(View.VISIBLE);
				holder.mLikeLayout.setVisibility(View.VISIBLE);
				holder.mUnopened_content.setVisibility(View.GONE);
				holder.mBottleContent.setVisibility(View.VISIBLE);
				if (!TextUtils.isEmpty(message.getBt_smallpic())) {

					if (message.getBt_pic_locked() == 0) {
						holder.mPhotoWrapper.setVisibility(View.VISIBLE);
						holder.mPhotoView.setVisibility(View.VISIBLE);
						holder.mLockedPhotoLayout.setVisibility(View.GONE);
						try {
							imageLoader.DisplayImage(message.getBt_smallpic(), holder.mPhotoView, R.drawable.album_nophoto);

						} catch (Exception e) {
							e.printStackTrace();
							holder.mPhotoView.setImageResource(R.drawable.album_nophoto);
						}

						holder.mPhotoView
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										byte[] tempBytes = null;
										try {
											String filename = ServiceUtils.convertUrlToFileName(message.getBt_smallpic());
											tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
													android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
										} catch (Exception e) {
											e.printStackTrace();
										}
										Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
										bigPicIntent.putExtra("pic_big", message.getBt_bigpic());
										bigPicIntent.putExtra("thumbnail", tempBytes);
										context.startActivity(bigPicIntent);
									}
								});
						
						if (message.getBt_allow_tuya() == 1) {
							holder.bt_drawing.setVisibility(View.VISIBLE);
							holder.bt_drawing.setOnClickListener(new View.OnClickListener() {
								
								@Override
								public void onClick(View v) {
									bottleFragment.position = position;
									bottleFragment.btId = message.getBtid();
									
									byte[] tempBytes = null;
									try {
										String filename = ServiceUtils.convertUrlToFileName(message.getBt_smallpic());
										tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
												android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
									} catch (Exception e) {
										e.printStackTrace();
									}
									
									Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
									bigPicIntent.putExtra("pic_big", message.getBt_bigpic());
									bigPicIntent.putExtra("thumbnail", tempBytes);
									bigPicIntent.putExtra("paintType", AppContext.PAINT_BT);
									bigPicIntent.putExtra("btid", message.getBtid());
									bigPicIntent.putExtra("return_type",BottleFragment.class.getSimpleName());
									bigPicIntent.putExtra("isToDraw", true);
									context.startActivity(bigPicIntent);			
									
								}
							});
						}else {
							holder.bt_drawing.setVisibility(View.GONE);
						}
						
						if (message.getBt_enable_tuya()== 0) {
							holder.bt_allow_draw.setVisibility(View.GONE);
						}else {
							holder.bt_allow_draw.setVisibility(View.VISIBLE);
							if (message.getBt_enable_tuya()== 1) {
								holder.bt_allow_draw.setBackgroundResource(R.drawable.btn_red_try);
								holder.bt_allow_draw.setText(TextUtil.R("bottle_txt_allow_draw"));
								holder.bt_allow_draw.setOnClickListener(new View.OnClickListener() {
									
									@Override
									public void onClick(View v) {
										bottleFragment.doPicPaint(message.getBtid(), 1);
									}
								});
							}else {
								holder.bt_allow_draw.setBackgroundResource(R.drawable.btn_gray_try);
								holder.bt_allow_draw.setText(TextUtil.R("bottle_txt_not_allow_draw"));
								holder.bt_allow_draw.setOnClickListener(new View.OnClickListener() {
									
									@Override
									public void onClick(View v) {
										bottleFragment.doPicPaint(message.getBtid(), 0);
									}
								});
							}
						}
					
					} else {
						holder.mLockedPhotoLayout.setVisibility(View.VISIBLE);
						holder.mPhotoWrapper.setVisibility(View.GONE);
						holder.mPhotoView.setVisibility(View.GONE);

						holder.mLockedPhotoTip
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										Intent unLockPhotoIntent = new Intent(context, ExpandEditActivity.class);
										AppContext.getInstance().setFromBottleTime(true);
										Bundle unLockBundle = new Bundle();
										unLockBundle.putString("from_type", AppContext.REPLY_BT);
										unLockBundle.putInt("bttype_id", message.getBtid());
										unLockPhotoIntent.putExtras(unLockBundle);
										context.startActivity(unLockPhotoIntent);
									}
								});
					}
				} else {
					holder.mPhotoWrapper.setVisibility(View.GONE);
					holder.mPhotoView.setVisibility(View.GONE);
					holder.mLockedPhotoLayout.setVisibility(View.GONE);
				}

				if (null != message.getBtlikes_list()) {
					holder.mLikeLayout.setVisibility(View.VISIBLE);

					if (message.getBt_liked() == 0) {
						holder.mLikeText.setTextColor(context.getResources().getColor(R.color.gray));
						holder.mLikeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill));
						holder.mLikeIcon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.like_gray2));
						// .setBackgroundResource(R.drawable.like_gray2);
					} else if (message.getBt_liked() == 1) {
						holder.mLikeText.setTextColor(context.getResources().getColor(R.color.gray));
						holder.mLikeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill));
						holder.mLikeIcon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.like_red2));
						// .setBackgroundResource(R.drawable.like_red2);
					} else if (message.getBt_liked() == 2) {
						holder.mLikeText.setTextColor(context.getResources().getColor(R.color.gray));
						holder.mLikeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill));
						holder.mLikeIcon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.like_black2));
						// .setBackgroundResource(R.drawable.like_black2);
					}

					if (message.getBt_like_enable() == 1) {
						holder.mLikeText.setTextColor(context.getResources().getColor(R.color.gray));
						holder.mLikeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill));
						holder.mLikeLayout
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										bottleFragment.showInfoPopupWindow(v, message.getBtid(), holder, position);
									}
								});
					} else {
						holder.mLikeText.setTextColor(context.getResources().getColor(R.color.white));
						holder.mLikeLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill_dark));
					}
					
					if (message.getBtlikes_count() == 0) {
						holder.mLikeText.setVisibility(View.GONE);
					} else {
						holder.mLikeText.setVisibility(View.VISIBLE);
						holder.mLikeText.setText(message.getBtlikes_count() + "");
					}
				} else {
					holder.mLikeLayout.setVisibility(View.GONE);
				}

				holder.mComment.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						bottleFragment.position = position;
						bottleFragment.btId = message.getBtid();
						Intent commentIntent = new Intent(context, ExpandEditActivity.class);
						AppContext.getInstance().setFromBottleTime(true);
						Bundle commentBundle = new Bundle();
						commentBundle.putString("from_type", AppContext.REPLY_BT);
						commentBundle.putInt("bttype_id", message.getBtid());
						commentIntent.putExtras(commentBundle);
						context.startActivity(commentIntent);
					}
				});

				if (message.getBt_copy_enable() == 1) {
					holder.mCopy.setVisibility(View.VISIBLE);
					holder.mCopy.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							bottleFragment.position = position;
							bottleFragment.btId = message.getBtid();
							Intent copyIntent = new Intent(context, ExpandEditActivity.class);
							AppContext.getInstance().setFromBottleTime(true);
							Bundle copyBundle = new Bundle();
							copyBundle.putString("from_type", AppContext.COPYMANUAL_BT);
							copyBundle.putInt("bttype_id", message.getBtid());
							copyIntent.putExtras(copyBundle);
							context.startActivity(copyIntent);
						}
					});
				} else {
					holder.mCopy.setVisibility(View.INVISIBLE);
				}

				if (message.getBt_allow_spread() == 1) {
					holder.mForward.setVisibility(View.VISIBLE);
					holder.mForward.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							final Context dialogContext = new ContextThemeWrapper(context,
									android.R.style.Theme_Light);
							bottleFragment.position = position;
							bottleFragment.btId = message.getBtid();
							String[] choices = new String[2];
							choices[0] = context.getString(R.string.bottle_txt_forward_to_friend);
							choices[1] = context.getString(R.string.bottle_txt_forward_to_platform);
							final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
									android.R.layout.simple_list_item_1, choices);

							final AlertDialog.Builder builder = new AlertDialog.Builder(
									dialogContext);
							builder.setTitle(R.string.system_info);
							builder.setSingleChoiceItems(adapter, -1,
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog, int which) {
											dialog.dismiss();
											if (which == 0) {
												
												Intent forwardIntent = new Intent(context, ChooseFriendActivity.class);
												Bundle forwardBundle = new Bundle();
												forwardBundle.putInt("choose_friend_type", AppContext.CHOOSE_FRIEND_FORWARD_BOTTLE_TYPE);
												forwardBundle.putInt("bttype_id", message.getBtid());
												forwardBundle.putString("return_type", BottleFragment.class.getSimpleName());
												forwardIntent.putExtras(forwardBundle);
												context.startActivity(forwardIntent);	
											} else {
												Intent forwardIntent = new Intent(context, ExpandEditActivity.class);
												AppContext.getInstance().setFromBottleTime(true);
												Bundle forwardBundle = new Bundle();
												forwardBundle.putString("from_type", AppContext.TRANSMIT_BT);
												forwardBundle.putInt("bttype_id", message.getBtid());
												forwardIntent.putExtras(forwardBundle);
												context.startActivity(forwardIntent);									}
										}
									});
							builder.create().show();
							
						}
					});
				} else {
					holder.mForward.setVisibility(View.INVISIBLE);
				}
				
				if (null != message.getBtlikes_list()
						&& message.getBtlikes_list().size() > 0) {
					holder.mLikeGrid.setVisibility(View.VISIBLE);
					holder.line_grid.setVisibility(View.VISIBLE);
					final GridLikeAdapter gridAdapter = new GridLikeAdapter(
							context, message.getBtlikes_list(), holder.mLikeGrid);
					holder.mLikeGrid.setAdapter(gridAdapter);
					holder.mLikeGrid
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
									BottleLikeEntry btLike = (BottleLikeEntry) gridAdapter.getItem(position);
									Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
									spaceIntent.putExtra("ouid", btLike.getLike_uid());
									context.startActivity(spaceIntent);
								}
							});
				} else {
					holder.mLikeGrid.setVisibility(View.GONE);
					holder.line_grid.setVisibility(View.GONE);
				}

				if (message.getBtfeeds_count() > 0) {
					holder.line_bt_comment.setVisibility(View.VISIBLE);
					holder.mfeed_item_comment_more.setVisibility(View.VISIBLE);

					holder.mfeed_item_comment_more
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									Intent bottleDetailIntent = new Intent(context, BottleDetailActivity.class);
									bottleDetailIntent.putExtra("btid", message.getBtid());
									bottleDetailIntent.putExtra("type", 1);// 2,location
									context.startActivity(bottleDetailIntent);
								}
							});

					holder.comments_ellipsis_text.setText(message
							.getBtfeeds_count()+"  " + TextUtil.R("bottle_txt_comment_more"));
				} else {
					holder.mfeed_item_comment_more.setVisibility(View.GONE);
					holder.line_bt_comment.setVisibility(View.INVISIBLE);
				}

				if (message.getBtfeeds_count() > 0) {
					holder.mfeed_comment_layout.setVisibility(View.VISIBLE);
					holder.mfeed_comment_layout.removeAllViews();

					for (int i = 0; i < message.getBtfeeds_list().size(); i++) {
						final BottleFeedEntry btFeedEntry = message.getBtfeeds_list().get(i);
						View mCommentView = LayoutInflater.from(context).inflate(
								R.layout.feed_comments_item, null);
						if (i == (message.getBtfeeds_list().size()-1)) {
							View feed_avatar = (View) mCommentView
									.findViewById(R.id.line_bttimeline_feed);
							feed_avatar.setVisibility(View.INVISIBLE);
						}
						mCommentView.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								Intent bottleDetailIntent = new Intent(context, BottleDetailActivity.class);
								bottleDetailIntent.putExtra("btid", message.getBtid());
								bottleDetailIntent.putExtra("type", 1);// 1，LikeList2,location
								context.startActivity(bottleDetailIntent);
							}
						});
						RemoteImageView feed_avatar = (RemoteImageView) mCommentView.findViewById(R.id.comment_profile_photo);
						if (!TextUtils.isEmpty(btFeedEntry.getFeed_avatar())) {

							try {
								feed_avatar.setDefaultImage(R.drawable.avatar_default_small);
								feed_avatar.setImageUrl(btFeedEntry.getFeed_avatar());
								feed_avatar.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
										spaceIntent.putExtra("ouid", btFeedEntry.getFeed_uid());
										context.startActivity(spaceIntent);
									}
								});
							} catch (Exception e) {
								e.printStackTrace();
								feed_avatar.setImageResource(R.drawable.avatar_default_small);
							}
						}
//						ImageView comment_link = (ImageView) mCommentView
//								.findViewById(R.id.iv_feed_comment_white);
//						if (btFeedEntry.getFeed_type() == 0) {
//							comment_link.setVisibility(View.VISIBLE);
//						} else {
//							comment_link.setVisibility(View.GONE);
//						}
						TextView comment_nickname = (TextView) mCommentView
								.findViewById(R.id.tv_feed_comment_nickname);
						String feed_name = null;
						if ((!TextUtils.isEmpty(btFeedEntry.getFeed_nickname()))|| (!TextUtils.isEmpty(btFeedEntry.getFeed_content()))) {
							if (btFeedEntry.getFeed_uid() != AppContext.getInstance()
									.getLogin_uid()) {
								feed_name = "<font color='#DA4A37'>"
										+ btFeedEntry.getFeed_nickname()
										+ "</font>";
							} else {
								feed_name = "<font color='#3F99D8'>"
										+ btFeedEntry.getFeed_nickname()
										+ "</font>";
							}
							
							String feed_content = btFeedEntry.getFeed_isnew()==1?("<font color='#DA4A37'>"
									+ TextUtil.htmlEncode(btFeedEntry.getFeed_content())
									+ "</font>"):TextUtil.htmlEncode(btFeedEntry.getFeed_content());
							
							try {
								comment_nickname.setText(Html.fromHtml(feed_name
										+ " " + feed_content));
							} catch (Exception e) {
								e.printStackTrace();
							}

						}

						TextView comment_time = (TextView) mCommentView
								.findViewById(R.id.tv_feed_comment_time);
						if ((!TextUtils.isEmpty(btFeedEntry.getFeed_time())) || (!TextUtils.isEmpty(btFeedEntry.getFeed_location()))) {
							String timeComent = (!TextUtils.isEmpty(btFeedEntry.getFeed_time())) ? ("#" + btFeedEntry.getFeed_time()): "";
							String location = (!TextUtils.isEmpty(btFeedEntry.getFeed_location())) ? ("@" + btFeedEntry.getFeed_location()): "";
							comment_time.setText(timeComent+" "+location);
							if (!TextUtils.isEmpty(location)) {
								comment_time.setOnClickListener(new View.OnClickListener() {
									
									@Override
									public void onClick(View v) {
										final Context dialogContext = new ContextThemeWrapper(context,
												android.R.style.Theme_Light);
										String[] choices = new String[1];
										choices[0] = context.getString(R.string.bottle_txt_check_location);

										final ListAdapter adapter = new ArrayAdapter<String>(
												dialogContext,
												android.R.layout.simple_list_item_1,
												choices);

										final AlertDialog.Builder builder = new AlertDialog.Builder(
												dialogContext);
										builder.setTitle(R.string.system_info);
										builder.setSingleChoiceItems(
												adapter,
												-1,
												new DialogInterface.OnClickListener() {
													public void onClick(
															DialogInterface dialog,
															int which) {
														dialog.dismiss();
														if (which == 0) {
															Intent intent = new Intent(context,MapViewActivity.class);
															intent.putExtra(
																	"longitude",btFeedEntry.getFeed_lng());
															intent.putExtra(
																	"latidute",btFeedEntry.getFeed_lat());
															intent.putExtra(
																	"location",btFeedEntry.getFeed_location());
															context.startActivity(intent);
														}
													}
												});
										builder.create().show();
									}
								});	
							}
						}
						RelativeLayout rl_bottle_comment_photo = (RelativeLayout)mCommentView.findViewById(R.id.rl_bottle_comment_photo);
						if (!TextUtils.isEmpty(btFeedEntry.getFeed_commentpic())) {
							rl_bottle_comment_photo.setVisibility(View.VISIBLE);
							ImageView feed_commentpic = (ImageView)mCommentView.findViewById(R.id.feed_comment_pic);
							try {
								imageLoader.DisplayImage(btFeedEntry.getFeed_commentpic(),feed_commentpic,
										R.drawable.album_nophoto);
								feed_commentpic
										.setOnClickListener(new View.OnClickListener() {

											@Override
											public void onClick(View v) {

												if (!TextUtils.isEmpty(btFeedEntry
														.getFeed_commentpic_tuya())) {
													showCommentPicFeedDialog(btFeedEntry);
												} else {
													byte[] tempBytes = null;
													try {
														String filename = ServiceUtils
																.convertUrlToFileName(btFeedEntry
																		.getFeed_commentpic());
														tempBytes = ServiceUtils
																.readStream(new FileInputStream(
																		new File(
																				android.os.Environment
																						.getExternalStorageDirectory()
																						+ "/"
																						+ AppContext.BottleTimelineIcon
																						+ "/"
																						+ filename)));
													} catch (Exception e) {
														e.printStackTrace();
													}
													Intent bigPicIntent = new Intent(context,
															PhotoDetailActivity.class);
													bigPicIntent.putExtra("pic_big",btFeedEntry.getFeed_commentpic_big());
													bigPicIntent.putExtra("thumbnail",
															tempBytes);
													context.startActivity(bigPicIntent);

												}
											
											}
										});
							} catch (Exception e) {
								e.printStackTrace();
								feed_commentpic.setImageResource(R.drawable.album_nophoto);
							}
						}else {
							rl_bottle_comment_photo.setVisibility(View.GONE);
						}
						
						TextView tv_feed_replycomment = (TextView)mCommentView.findViewById(R.id.tv_feed_replycomment);
						if (!TextUtils.isEmpty(btFeedEntry.getFeed_replycomment())) {
							tv_feed_replycomment.setVisibility(View.VISIBLE);
							tv_feed_replycomment.setText(btFeedEntry.getFeed_replycomment());
						} else {
							tv_feed_replycomment.setVisibility(View.GONE);
						}
						
						holder.mfeed_comment_layout.addView(mCommentView);
					}
				} else {
					holder.mfeed_comment_layout.setVisibility(View.GONE);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	private void showCommentPicFeedDialog(final BottleFeedEntry  message) {
		String[] choices = new String[]{TextUtil.R("bottle_txt_feed_comment_pic"),TextUtil.R("bottle_txt_feed_comment_tuya")};
		final Context dialogContext = new ContextThemeWrapper(context, android.R.style.Theme_Light);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {

							byte[] tempBytes = null;
							try {
								String filename = ServiceUtils.convertUrlToFileName(message.getFeed_commentpic());
								tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
										android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
							} catch (Exception e) {
								e.printStackTrace();
							}
							Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
							bigPicIntent.putExtra("pic_big", message.getFeed_commentpic_big());
							bigPicIntent.putExtra("thumbnail", tempBytes);
							context.startActivity(bigPicIntent);
							
						} else {
							Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
							bigPicIntent.putExtra("pic_big", message.getFeed_commentpic_tuya());
							context.startActivity(bigPicIntent);
						}
					}
				});
		builder.create().show();
	}
	
	public static class ViewHolder {

		public View line_grid;
		public View line_bt_comment;
		TextView text;
		TextView time;
		TextView status;

		int btId;
		int btTypeId;
		String btBigPic;
		int ouid;
		String nickname;

		public RemoteImageView mAuthorView;// 用户头像
		public ImageView mBottleType;// 瓶子类型

		public TextView mNickname;// 扔瓶子用户昵称
		// public TextView mFeed;// 扔瓶子、传递瓶子动态
		// public TextView mTime;// 扔瓶子时间
		public TextView mLocation;// 瓶子地点
		public TextView mDistance;// 瓶子距离

		public TextView mTag;// 瓶子标签
		public TextView mCopyTips;// 传递信息（显示在瓶子标签下边）
		public TextView mood;// 瓶子心情

		public RelativeLayout mBottleContent;// 显示瓶子的内容
		public TextView mMessage;//
		public LinearLayout mPhotoWrapper;// photo外套
		public ImageView mPhotoView;// 瓶子内涵图

		public LinearLayout mLockedPhotoLayout;// 被锁图片内容区
		public ImageView mLockedPhoto;// 上锁的瓶子图
		public Button mLockedPhotoTip;// 上传图片解锁
		public Button bt_drawing;//涂鸦
		public Button bt_allow_draw;//允许涂鸦

		public LinearLayout mUnopened_content;// 瓶子没打开时
		public TextView mOpen_tips;
		public Button mOpen;
		public Button mDrop;

		public RelativeLayout mLikeLayout;
		public ImageView mLikeIcon;// 喜欢
		public TextView mLikeText;// 喜欢数量

		public CustomGridView mLikeGrid;// 喜欢头像区域
		public RelativeLayout mFeedComment;// 回复区域
		public Button mComment;//
		public Button mCopy;//
		public Button mForward;//

		public RelativeLayout mfeed_item_comment_more;// 查看更多留言
		public TextView comments_ellipsis_text;//
		public LinearLayout mfeed_comment_layout;// 留言
		
		public RelativeLayout rl_bottle_comment_photo;
		public ImageView feed_comment_pic;

		public LinearLayout mOp_layout;// 我收到的这个瓶子的动态，不是我收到的瓶子不显示
		public RemoteImageView mOpAvatar;
		public LinearLayout mOp_location;
		public TextView mFeedContent;
		public TextView mOplocation;
		public TextView mOpDistance;
		int flag = -1;
	}

}
